package Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Flights.Flights;
import User.User;
import User.Users;

/**
 * Servlet implementation class LogInServlet
 */
@WebServlet("/LogInServlet")
public class LogInServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogInServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");

		PrintWriter out = response.getWriter(); 
		
		ServletContext ctx = getServletContext();
		HttpSession session = request.getSession();
		
		String username = request.getParameter("username");
		String client = request.getParameter("client");
		if(client==null){
			client="web";
		}
		
		Users users = (Users) session.getAttribute("users");
		if(users==null){
			users =	new Users(ctx.getRealPath(""));
		}
		
		String logOff = request.getParameter("logOff");
		if(logOff!=null && logOff.equals("true")){
			users.getUser().logOff();
			
			session.setAttribute("users", null);
			session.setAttribute("user", null);
			
			if(client.equals("CommandPrompt")){
				out.println("Uspesno ste se odjavili"); 
				out.close();
			} else {
				RequestDispatcher disp = getServletContext().getRequestDispatcher("/prijava.faces");
				disp.forward(request, response);
			}
			return;
		}

		if(username!=null){
			for(User u : users.getUsers()){
				if(username.equals(u.getUsername()))
					users.setUser(u);
					
			}
		
			if(users.getUser()!=null){
				users.getUser().setLogin(true);
				session.setAttribute("users", users);

				Flights flights = new Flights(ctx.getRealPath("") + "/listaLetova/flights.txt");
				
				session.setAttribute("flights", flights);
				
				if(client.equals("CommandPrompt")){
					out.println("Uspesno ste se prijavili"); 
					out.close();
				} else {
					response.sendRedirect("main.faces");
				}
				return;
			} else {
				session.setAttribute("user", "nePostoji");
			
				if(client.equals("CommandPrompt")){
					out.println("Korisnicko ime ne postoji"); 
					out.close();
				} else {
					RequestDispatcher disp = getServletContext().getRequestDispatcher("/prijava.faces");
					disp.forward(request, response);
				}
				return;
			}
		} else {
			session.setAttribute("user", null);
			
			if(client.equals("CommandPrompt")){
				out.println("GRESKA"); 
				out.close();
			} else {
				RequestDispatcher disp = getServletContext().getRequestDispatcher("/prijava.faces");
				disp.forward(request, response);
			}
			return;
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
